package com.demo.starter.interceptor;

import com.demo.starter.domain.vo.ArticleCategoryVO;
import com.demo.starter.service.IArticleCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Web 页面渲染 拦截器 </br>
 */
public class WebMvcAttributeInterceptor implements HandlerInterceptor {

    @Autowired
    private IArticleCategoryService articleCategoryService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 不做处理返回true
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        final List<ArticleCategoryVO> menuCategories = articleCategoryService.listMenuCategories();
        // 添加全局页面属性 菜单项中的栏目列表
        if (modelAndView != null) {
            modelAndView.addObject("menuCategories", menuCategories);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
